package org.farm2.service.controller;

import org.farm2.base.domain.FarmUserContextLoader;
import org.farm2.files.service.ResourceFileServiceInter;
import org.farm2.skc.dto.web.WebTagDto;
import org.farm2.skc.domain.SkcTag;
import org.farm2.skc.service.SkcKnowServiceInter;
import org.farm2.skc.service.SkcKnowTagServiceInter;
import org.farm2.skc.service.SkcTagServiceInter;
import org.farm2.tools.bean.FarmBeanUtils;
import org.farm2.tools.web.FarmResponseCode;
import org.farm2.tools.web.FarmResponseResult;
import org.farm2.tools.web.dto.SearchKeyDto;
import org.farm2.wdap.service.WdapExtendFileServiceInter;
import org.farm2.wdap.service.WdapTaskServiceInter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * 知识
 *
 * @author cbtg自动生成  2025-2-4 12:13:51
 */
@RestController
@RequestMapping("/api/wtag")
public class WebTagController {
    @Autowired
    private SkcKnowServiceInter skcKnowServiceImpl;
    @Autowired
    private WdapExtendFileServiceInter wdapExtendFileServiceImpl;
    @Autowired
    private ResourceFileServiceInter resourceFileServiceImpl;
    @Autowired
    private WdapTaskServiceInter wdapTaskServiceImpl;
    @Autowired
    private SkcKnowTagServiceInter skcKnowTagServiceImpl;
    @Autowired
    private SkcTagServiceInter skcTagServiceImpl;

    /**
     * 查询系统标签
     *
     * @param key
     * @return
     */
    @PreAuthorize("@farmAction.has('wtag.search')")
    @PostMapping("/search")
    public FarmResponseResult queryAll(@RequestBody SearchKeyDto key) {
        List<SkcTag> tags = skcTagServiceImpl.getTags(key.getKey());
        List<WebTagDto> bTags = new ArrayList<>();
        for (SkcTag tag : tags) {
            WebTagDto td = new WebTagDto();
            FarmBeanUtils.copyProperties(tag, td);
            td.setType("2");
            bTags.add(td);
        }
        if (tags.size() <= 0) {
            WebTagDto noneTag = new WebTagDto();
            noneTag.setType("3");
            noneTag.setTagkey(key.getKey());
            noneTag.setName(key.getKey());
            bTags.add(noneTag);
        }
        return FarmResponseResult.getInstance(FarmResponseCode.SUCESS, bTags);
    }


    
    @PreAuthorize("@farmAction.has('wtag.my')")
    @PostMapping("/my")
    public FarmResponseResult my() {

        List<WebTagDto> tags = skcTagServiceImpl.getUserTags();

        if (FarmUserContextLoader.isLogin()) {
            tags.add(new WebTagDto("0", "我的知识", "SYS", skcKnowServiceImpl.getKnowNumByUser(FarmUserContextLoader.getCurrentUserKey())));
        }
        tags.add(new WebTagDto("0", "最新知识", "SYS", skcKnowServiceImpl.getNewKnowsNum(FarmUserContextLoader.getCurrentUserKey())));
        tags.sort(new Comparator<WebTagDto>() {
            @Override
            public int compare(WebTagDto o1, WebTagDto o2) {
                return o1.getType().compareTo(o2.getType());
            }
        });
        return FarmResponseResult.getInstance(FarmResponseCode.SUCESS, tags);
    }
}
